package com.wh.stock.controller;

import com.wh.basic.vo.AjaxResult;
import com.wh.stock.service.IStockLogService;
import com.wh.stock.pojo.StockLog;
import com.wh.stock.dto.StockLogQueryDto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/stockLog")
public class StockLogController {

    @Autowired
    public IStockLogService stockLogService;

    /**
     * 接口：添加或修改
     * @param stockLog 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody StockLog stockLog) {
        if ( stockLog.getId() != null){
            stockLogService.updateById(stockLog);
        }else{
            stockLogService.save(stockLog);
        }
        return new AjaxResult();

    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        stockLogService.removeById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        stockLogService.removeByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @PostMapping("/{id}")
    public StockLog findOne(@PathVariable("id") Long id) {
        return stockLogService.getById(id);
    }

    /**
     *查询库存更新记录
     * @param procId
     * @return
     */
    @GetMapping ("/{procId}")
    public List<StockLog> findStockLog(@PathVariable("procId") Long procId) {
        return stockLogService.findStockLog(procId);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<StockLog> findAll() {
        return stockLogService.list();
    }


    /**
     * 接口：分页查询或高级查询
     * @param stockLogQueryDto 查询对象
     * @return IPage<StockLog> 分页对象
     */
    @PostMapping
    public IPage<StockLog> findByPage(@RequestBody StockLogQueryDto stockLogQueryDto) {
        return stockLogService.findByPage(stockLogQueryDto);
    }
}
